# -*- coding: utf-8 -*-
from tg import config
from lifepyscle.model import *
import os,shutil
from random import random,randint

def ean13():
    valid_ean13 = False
    while not valid_ean13:
        key = str(random())[2:]
        new_ean13 = "%s%s"%(key,str(randint(1,10)))
        try:
            existingEAN13 = DBSession.query(Record.ean13).filter(Record.ean13==new_ean13).one()
        except: # doesn't exist - good
            ean13 = new_ean13
            valid_ean13 = True
        else:
            continue
    if valid_ean13:
        return ean13
    else:
        raise StandardError,"could not create a unique valid ean13 barcode number"

